package io.ede.common.utils;

import org.apache.commons.vfs2.FileNotFoundException;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Map;

public class RarUtils {

    /**
     * 采用命令行方式解压文件
     * @param zipFile 压缩文件
     * @param destDir 解压结果路径
     * @return
     */
    public static boolean realExtract(File zipFile, String destDir) {
        // 解决路径中存在/..格式的路径问题
        destDir = new File(destDir).getAbsoluteFile().getAbsolutePath();
        while(destDir.contains("..")) {
            String[] sepList = destDir.split("\\\\");
            destDir = "";
            for (int i = 0; i < sepList.length; i++) {
                if(!"..".equals(sepList[i]) && i < sepList.length -1 && "..".equals(sepList[i+1])) {
                    i++;
                } else {
                    destDir += sepList[i] + File.separator;
                }
            }
        }

        // 获取WinRAR.exe的路径，放在java web工程下的WebRoot路径下
        String classPath = "";
        try {
            classPath = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath();
        } catch (URISyntaxException e1) {
            e1.printStackTrace();
        }

        String winrarPath = "C:\\Program Files\\WinRAR\\WinRAR.exe";
        System.out.println(winrarPath);

        boolean bool = false;
        if (!zipFile.exists()) {
            return false;
        }

        // 开始调用命令行解压，参数-o+是表示覆盖的意思
        String cmd = winrarPath + " X -o+ " + zipFile + " " + destDir;
        System.out.println(cmd);
        try {
            Process proc = Runtime.getRuntime().exec(cmd);
            if (proc.waitFor() != 0) {
                if (proc.exitValue() == 0) {
                    bool = false;
                }
            } else {
                bool = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("解压" + (bool ? "成功" : "失败"));
        return bool;
    }

    /**
     * 将文件转换成byte数组
     * @param tradeFile
     * @return
     */
    public static byte[] File2byte(File tradeFile){
        byte[] buffer = null;
        try
        {
            FileInputStream fis = new FileInputStream(tradeFile);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1)
            {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        return buffer;
    }

    public static void getFileList(String basePath,File file,Map map) {
        //String basePath = file.getPath();
        //basePath = basePath;
        File[] files = file.listFiles();
        for (File f : files) {
            //如果文件夹下有子文件夹，获取子文件夹下的所有文件全路径。
            if (f.isDirectory()) {
                getFileList(basePath,f,map);
            }else{
                String _path = f.getPath();
                _path = _path.replace(basePath,"");
                _path = _path.replace("\\","/");
                map.put(_path,File2byte(f));
            }
        }
    }

    public static void main(String[] args) {
        File file = new File("d://test.rar");
        try {
            RarUtils.realExtract(file,"d:\\");
            /*for(Object keyObj: map.keySet()) {
                String key = keyObj.toString();
                byte[] value = (byte[]) map.get(keyObj);
                System.out.println(key);
                System.out.println("文件内容：" + new String(value));
            }*/
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




}
